<?php

class TaikhoansController extends BaseController {

    /**
     * Tai_khoan Repository
     *
     * @var Tai_khoan
     */
    protected $tai_khoan;

    public function __construct(Tai_khoan $tai_khoan) {
        $this->tai_khoan = $tai_khoan;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {

        $tai_khoans = $this->tai_khoan->where('auth_type','manager')->get();

        return View::make('tai_khoans.index', compact('tai_khoans'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        return View::make('tai_khoans.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        
        $input = Input::all();
        $validation = Validator::make($input, Tai_khoan::$rules);
        $input['password'] = Hash::make($input['password']);
        $input['auth_type'] = 'manager';
        // return $input;
        if ($validation->passes()) {
            $this->tai_khoan->create($input);
            return Redirect::route('accounts.index');
        return Redirect::route('accounts.create')
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }
}
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        $tai_khoan = $this->tai_khoan->findOrFail($id);

        return View::make('tai_khoans.show', compact('tai_khoan'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $tai_khoan = $this->tai_khoan->find($id);

        if (is_null($tai_khoan)) {
            return Redirect::route('accounts.index');
        }

        return View::make('tai_khoans.edit', compact('tai_khoan'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        $current_pass = DB::table('tai_khoans')->where('id',$id)->pluck('password');

        $current_username = DB::table('tai_khoans')->where('id',$id)->pluck('username');
        $input = array_except(Input::all(), '_method');
        // $validation = Validator::make($input, Tai_khoan::$rules);
        // $input['password'] = Hash::make($input['password']);
        $new_pass = $input['password'];

            if($new_pass == $current_pass && $input['username'] == $current_username){
                $tai_khoan = $this->tai_khoan->find($id);
                $tai_khoan->update(
                    array(
                        'mascb' => $input['mascb'],
                        'ho_cb' => $input['ho_cb'],
                        'ten_cb' => $input['ten_cb'],
                        'ngay_sinh_cb' => $input['ngay_sinh_cb'],
                        'gioi_tinh_cb' => $input['gioi_tinh_cb'],
                        'email_cb' => $input['email_cb'])
                    );
               return Redirect::route('accounts.show', $id);
             }
             elseif($new_pass != $current_pass && $input['username'] == $current_username){
             
                $validation = Validator::make($input, Tai_khoan::$rules_user);
                $input['password'] = Hash::make($input['password']);
                if ($validation->passes()) {
                    $tai_khoan = $this->tai_khoan->find($id);
                    $tai_khoan->update($input);
                    return Redirect::route('accounts.show', $id);
                }
                return Redirect::route('accounts.edit', $id)
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');    

            } elseif($new_pass == $current_pass && $input['username'] != $current_username){
              
                $validation = Validator::make($input, Tai_khoan::$rules_pass);
                $input['password'] = Hash::make($input['password']);
                if ($validation->passes()) {
                    $tai_khoan = $this->tai_khoan->find($id);
                    $tai_khoan->update($input);
                    return Redirect::route('accounts.show', $id);
                }
                return Redirect::route('accounts.edit', $id)
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
            }else{
                $validation = Validator::make($input, Tai_khoan::$rules);
                $input['password'] = Hash::make($input['password']);
                if ($validation->passes()) {
                    $tai_khoan = $this->tai_khoan->find($id);
                    $tai_khoan->update($input);
                    return Redirect::route('accounts.show', $id);
                }
                return Redirect::route('accounts.edit', $id)
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');    
            
            }
        }
            
        
            
 

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        $this->tai_khoan->find($id)->delete();

        return Redirect::route('accounts.index');
    }

}
